<html>
<body>
UID:403994037
<br/>
UID:503993985
<form action="calculator.php" method="post">
<input type="text" name="inputStr" />
<input type="submit" value="Calculate" />

<?php

if(isset($_POST['inputStr']))
{


$array[0]=4;
$array[1]=5;

//echo $array[0];

$array[0]=3;
$array[3]=3;

//echo "n";
//echo $array[0].$array[1].$array[2].$array[3];

function checkPre($op1,$op2)
{


switch($op1)
{
case "+":
case "-":
$op1Val=1;
break;

case "*":
case "/":
$op1Val=2;
break;
}

switch($op2)
{
case "+":
case "-":
$op2Val=1;
break;

case "*":
case "/":
$op2Val=2;
break;
}

return $op1Val-$op2Val;

}

function evaluate($op1,$op2,$operand)
{
switch($operand)
{
case "+":
return $op1+$op2;

case "-":

$returnVal=$op1-$op2;
return $returnVal;

case "*":
return $op1*$op2;
}

if($operand=="/" && $op2=="0")
{	//echo "ada" ;
	return "Exception";
}

else
{
return $op1/$op2;
}
}



function calc()
{
$ipString= $_POST['inputStr'];


//$pattern= "((-?[0-9]+[\.[0-9]]?([0-9])*[\+\*\/-])*)[-]?[0-9]+[\.[0-9]]?([0-9])*";

$pattern= "/^(-?[0-9]+([\.][0-9]+)?[\+\*\/-])*-?[0-9]+([\.][0-9]+)?$/";

//$pattern= "(-?[0-9]+[\.[0-9]]?([0-9])+[\+\*\/-])?[0-9]+[\.[0-9]]?([0-9])";
//echo $ipString;
if(!preg_match($pattern,$ipString))
	return "Invalid input Expression ".$ipString;

//echo checkPre("+","*");

$tops1=-1;
$tops2=-1;

$array1[0]=0;
$array2[0]=0;



$number="";
for($i=0;$i<strlen($ipString);$i++)
{
$currentChar =  $ipString[$i];
if(is_numeric($currentChar) || $currentChar=="." || (($currentChar=="-" &&
$i==0)|($currentChar=="-" && !is_numeric($ipString[$i-1]))))
{
/*
if($currentChar=="-" && $i==0)
{
	$number=$number."".$currentChar;
	continue;
}

else if ($currentChar=="-" && !is_numeric($ipString[$i-1]))
{
	$number=$number."".$currentChar;
	continue;
}
else if(is_numeric($currentChar) || $currentChar==".") 
{
$number=$number."".$currentChar;
}
*/

$number=$number."".$currentChar;
}
else
{
	$array1[++$tops1]=$number;
	$number="";

	if($tops2!=-1 && (checkPre($currentChar,$array2[$tops2])>0))
		$array2[++$tops2]=$currentChar;
	else if ($tops2==-1)
		$array2[++$tops2]=$currentChar;
	else
	{	
		$popOperator=$array2[$tops2--];
		$array2[++$tops2]=$currentChar;
		$operand2=$array1[$tops1];
		$operand1=$array1[--$tops1];
		//if($popOperator=="/"&& $operand2=="0")
		//	return "Divide by zero not allowed";
		//else	
		$val=evaluate($operand1,$operand2,$popOperator);
	//	echo "value are".$val;
		if($val=="Exception")
		{
		//echo "return";
		//return;
		$val=0;
		}
		//echo "eval ".eval($operand1.$popOperator.$operand2.";")." end";
		$array1[$tops1]=$val;
	///	echo $val;
	}


}


}
$array1[++$tops1]=$number;
while($tops2!=-1){

$popOperator=$array2[$tops2--];
//$array2[++$tops2]=$currentChar;
$operand2=$array1[$tops1];
$operand1=$array1[--$tops1];

$val=evaluate($operand1,$operand2,$popOperator);

if($val=="Exception")
{
//echo "return";
//return;
if($tops2!=-1)
$val=0;
else
$val="";
}

$array1[$tops1]=$val;
//echo "value is ".$val."<br/>";
}
if($tops1==0)
	$val=$array1[0];
return $ipString." = ".$val;
/*
for($i=0;$i<sizeof($array1);$i++)
	echo $array1[$i];

for($i=0;$i<sizeof($array2);$i++)
	echo $array2[$i];
*/
}

echo "<h1>Result</h1>".calc();
}
?>

</body>
</html>
